#include "frmsetting.h"
#include "ui_frmsetting.h"
#include "frmuserlist.h"
#include "frmdataset.h"
#include "iconhelper.h"

FrmSetting::FrmSetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FrmSetting)
{
    ui->setupUi(this);

    this->initForm();
}

FrmSetting::~FrmSetting()
{
    delete ui;
}

void FrmSetting::initForm()
{
    ui->page1_horlayout->addWidget(new FrmUserlist);
    ui->page2_horlayout->addWidget(new FrmDataSet);

    initStyle();
    initLeftMain();
    ui->btnUserlist->click();
}

void FrmSetting::initStyle()
{
    //加载样式表
    QString qss;
    QFile file(":/qss/blacksoft.css");
    if(file.open(QFile::ReadOnly))
    {
        qss=QLatin1String(file.readAll());
        QString palatteColor=qss.mid(20,7);
        qApp->setPalette(QPalette(palatteColor));
        qApp->setStyleSheet(qss);
        file.close();
    }

    //先从样式表中取出对应的颜色
    QString textColor, panelColor, borderColor, normalColorStart, normalColorEnd, darkColorStart, darkColorEnd, highColor;
    getQssColor(qss, textColor, panelColor, borderColor, normalColorStart, normalColorEnd, darkColorStart, darkColorEnd, highColor);

    //将对应的颜色设置到控件
    this->borderColor=highColor;
    this->normalBgColor=normalColorStart;
    this->darkBgColor=darkBgColor;
    this->normalTextColor=textColor;
    this->darkTextColor=darkColorStart;
}

void FrmSetting::getQssColor(const QString &qss, const QString &flag, QString &color)
{
    int index=qss.indexOf(flag);
    if(index>=0)
    {
        color=qss.mid(index+flag.length(),7);
    }
}

void FrmSetting::getQssColor(const QString &qss, QString &textColor, QString &panelColor,
                          QString &borderColor, QString &normalColorStart,
                          QString &normalColorEnd, QString &darkColorStart, QString &darkColorEnd, QString &highColor)
{
    getQssColor(qss,"TextColor:",textColor);
    getQssColor(qss, "PanelColor:", panelColor);
    getQssColor(qss, "BorderColor:", borderColor);
    getQssColor(qss, "NormalColorStart:", normalColorStart);
    getQssColor(qss, "NormalColorEnd:", normalColorEnd);
    getQssColor(qss, "DarkColorStart:", darkColorStart);
    getQssColor(qss, "DarkColorEnd:", darkColorEnd);
    getQssColor(qss, "HighColor:", highColor);
}

void  FrmSetting::initLeftMain()
{
    iconMains << 0xe64b << 0xe677;
    btnsLeft<<ui->btnUserlist<<ui->btnDataManage;

    int count = btnsLeft.count();
    for (int i = 0; i < count; ++i) {
        QToolButton *btn = (QToolButton *)btnsLeft.at(i);
        btn->setCheckable(true);
        btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        connect(btn, SIGNAL(clicked(bool)), this, SLOT(leftClick()));
    }

    IconHelper::StyleColor styleColor;
    styleColor.position = "left";
    styleColor.iconSize = 18;
    styleColor.iconWidth = 35;
    styleColor.iconHeight = 25;
    styleColor.borderWidth = 4;
    styleColor.borderColor = borderColor;
    styleColor.setColor(normalBgColor, normalTextColor, darkBgColor, darkTextColor);
    IconHelper::setStyle(ui->widgetleft, btnsLeft, iconMains, styleColor);
}

void FrmSetting::leftClick()
{
    QAbstractButton *btn=(QAbstractButton *)sender();
    int index=btnsLeft.indexOf(btn);
    ui->stackedWidget->setCurrentIndex(index);
    foreach (QAbstractButton *b, btnsLeft) {
        b->setChecked(btn==b);
    }
}
